package com.zjsru.oneDay;

/**
 * 1128. 等价多米诺骨牌对的数量
 *
 * @Author: cookLee
 * @Date: 2025-05-04
 */
public class NumEquivDominoPairs {
    /**
     * 主
     * \
     * 输入：dominoes = [[1,2],[2,1],[3,4],[5,6]]
     * 输出：1
     * \
     * 输入：dominoes = [[1,2],[1,2],[1,1],[1,2],[2,2]]
     * 输出：3
     * \
     *
     * @param args args
     */
    public static void main(String[] args) {
        NumEquivDominoPairs numEquivDominoPairs = new NumEquivDominoPairs();
        int[][] dominoes = new int[][]{{1, 2}, {2, 1}, {3, 4}, {5, 6}};
        System.out.println(numEquivDominoPairs.numEquivDominoPairs2(dominoes));
    }


    public int numEquivDominoPairs(int[][] dominoes) {
        int ans = 0;
        for (int i = 0; i < dominoes.length; i++) {
            for (int j = i + 1; j < dominoes.length; j++) {
                if (this.helper(dominoes[i], dominoes[j])) {
                    ans++;
                }
            }
        }
        return ans;
    }

    private boolean helper(int[] pre, int[] after) {
        return (pre[0] == after[0] && pre[1] == after[1]) || (pre[1] == after[0] && pre[0] == after[1]);
    }

    public int numEquivDominoPairs2(int[][] dominoes) {
        int ans = 0;
        int[] count = new int[101];
        for (int[] dominoe : dominoes) {
            int key = dominoe[0] > dominoe[1] ? dominoe[0] * 10 + dominoe[1] : dominoe[1] * 10 + dominoe[0];
            ans += count[key];
            count[key]++;
        }
        return ans;
    }

}
